<?php
/**
 * 系统相关类
 * author: heimuya
 * time: 2014-4-15
 */
 namespace Admin\Controller;
 use Think\Controller;
 
class SysController extends CommonController {
	
	public function _initialize(){
		parent::_initialize();
	}
	
	public function sysset(){
		$web = C('WEBSET');
		$this->assign('web',$web);
		$this->display();
	}
	
	public function dochange(){
		$upload = new \Think\Upload();// ;// 实例化上传类
		$upload->maxSize = 204800 ; // 设置附件上传大小 
        $upload->exts = array('jpg', 'gif', 'jpeg','png'); // 设置附件上传类型 
        $upload->autoSub = FALSE; // 设置附件上传类型
		$upload->savePath = './static/'; // 设置附件上传目录 
		$info = $upload->upload();
		if($info) {
			$webset['sitelogo'] = __ROOT__.'/Uploads/static/'.$info['logo']['savename'];
		}else {
			$webset['sitelogo'] = __ROOT__.'/Uploads/static/logo.png';
		}
		$webset['open'] = I('open');
		$webset['tips'] = I('tips');
		$webset['tips_en'] = I('tips_en');
		$webset['sitename'] = I('sitename');
		$webset['sitename_en'] = I('sitename_en');
		$webset['siteurl'] = I('siteurl');
		$webset['copyright'] = I('copyright');
		$webset['copyright_en'] = I('copyright_en');
		$webset['sitetongji'] = I('sitetongji');
		$webset['beian'] = I('beian');
		//处理必要数据约束
		if($webset['sitename'] == '' || $webset['sitename_en'] == '' || $webset['siteurl'] == '' || $webset['copyright'] == '' || $webset['copyright_en'] == ''){
			$this->error("网站名称，网站地址，版权不能为空，请完善！");
			exit();	
		}
		if($webset['open'] == '0'){
			if($webset['tips'] == '' || $webset['tips_en']){
				$this->error("您选择的关闭网站，请填写关闭原因");
			}
		}
		$tempurl = $webset['siteurl'];//处理url规范
		$tempurlb = $tempurl;
		$tempurl = substr($tempurl, 0,7);
		$tempurlb = substr_count($tempurlb, '/');
		if($tempurl != "http://" || $tempurlb != 2){
			$this->error("网站地址不符合标准，前面必须加http://并且不能以‘/’结尾，请核实");
			exit();
		}
		$webset['copyright'] = str_replace("'", '"',  $webset['copyright']);//处理引号规范
		$webset['copyright_en'] = str_replace("'", '"',  $webset['copyright']);//处理引号规范
		$webset['sitetongji'] = str_replace( "'",'"', $webset['sitetongji']);
//		if($webset['webemail'] != ''){//处理邮箱规范
//			$flag = strpos($webset['webemail'], '@');
//			if($flag == false || $flag == 0){
//				$this->error("如果填写邮箱，请使用正确格式");
//			}
//		}
		
		$add = array(
			'WEBSET'	=> $webset,
		);
		$config_old=require 'system.inc.php';
		$config_new=array_merge($config_old,$add);
		arr2file('./system.inc.php',$config_new);
		oplog('修改系统信息设置',$_SESSION['auser'],1);
		$this->_cache();
		$this->success("设置成功，稍等为您跳转");
	}
	
	public function comset(){
		$com = C('COM');
		C('TOKEN_ON',false);
		$this->assign('com',$com);
		$this->display();
	}
	
	public function changecom(){
		$comset = I('post.');
		if($comset['comname'] == '' || $comset['comname_en'] == '' ||$comset['comaddress'] == '' ||$comset['comaddress_en'] == '' ||$comset['com_tel'] == '' ||$comset['com_email'] == '' ||$comset['com_fax'] == '' ||$comset['com_zipcode'] == ''){
			$this->error('每一项都很重要，请不要为空');
		}
		$add = array(
			'COM'  => $comset,
		);
		$config_old=require './system.inc.php';
		$config_new=array_merge($config_old,$add);
		arr2file('./system.inc.php',$config_new);
		oplog('修改联系信息设置',$_SESSION['auser'],1);
		$this->_cache();
		$this->success("设置成功，稍等为您跳转");
	}
	
	public function seoset(){
		$seo = C('SEO');
		C('TOKEN_ON',false);
		$this->assign('seo',$seo);
		$this->display();
	}
	
	public function changeseo(){
		$seoset = I('post.');
		$add = array(
			'SEO'  => $seoset,
		);
		$config_old=require './system.inc.php';
		$config_new=array_merge($config_old,$add);
		arr2file('./system.inc.php',$config_new);
		oplog('修改SEO设置',$_SESSION['auser'],1);
		$this->_cache();
		$this->success("设置成功，稍等为您跳转");
	}
	
	public function smtpset(){
		C('TOKEN_ON',false);
		$this->display();
	}
	
	public function changesmtp(){
		$seoset = I('post.');
		$add = array(
			'MAIL_USERNAME'  => $seoset['sendemail'],
			'MAIL_HOST'  => $seoset['smtp'],
			'MAIL_PASSWORD'  => $seoset['pass'],
			'MAIL_CHARSET'  => 'UTF-8',
			'MAIL_SMTPAUTH'  => ($seoset['auth'] == '1') ? TRUE : FALSE,
			'MAIL_ISHTML'  => ($seoset['html'] == '1') ? TRUE : FALSE,
		);
		$config_old=require './system.inc.php';
		$config_new=array_merge($config_old,$add);
		arr2file('./system.inc.php',$config_new);
		oplog('修改邮件服务设置',$_SESSION['auser'],1);
		$this->_cache();
		$this->success("设置成功，稍等为您跳转");
	}
	
	public function _cache(){
		//RUNTIME_FILE常量是入口文件中配置的runtimefile的路径及文件名；
		if(file_exists(RUNTIME_FILE)){
    		unlink(RUNTIME_FILE); //删除RUNTIME_FILE;
		}
		//光删除runtime_file还不够，要清空一下Cache文件夹中的文件；代码如下：
		$cachedir=RUNTIME_PATH."/Cache/";   //Cache文件的路径；
		if ($dh = opendir($cachedir)) {     //打开Cache文件夹；
    		while (($file = readdir($dh)) !== false) {    //遍历Cache目录，
            	unlink($cachedir.$file);                //删除遍历到的每一个文件；
    		}
    		closedir($dh);
		}       
	}
}
